﻿Imports System.Data
Imports System.Data.Odbc
Imports System.Text
Imports System.Collections.Generic
Imports WSEntidades
Imports Utilerias
Imports WSFactory
Imports MySql.Data.MySqlClient

Public Class TipoDocumentosDAL
    Private cadConex As String = ""

    Public Sub New(ByVal pCadConex As String)
        Me.cadConex = pCadConex
    End Sub

#Region "FuncionesUtilizables"

    Public Function actualizaTipoDocumentos(ByVal pTipoDocumentos As TipoDocumentosBE) As Boolean
        Try
            Using myConn As MySqlConnection = New MySqlConnection(Me.cadConex)
                Using myComm As MySqlCommand = myConn.CreateCommand()
                    myComm.CommandText = "UPDATE tipodoctos " & _
                                         "SET UsaFA = @UsaFA, RegFa = @RegFA, UsaCP = @UsaCP, RegCP = @RegCP, " & _
                                         "UsaRH = @UsaRH, RegRH = @RegRH, UsaRA = @UsaRA, RegRA = @RegRA, " & _
                                         "UsaNA = @UsaNA, RegNA = @RegNA, UsaNC = @UsaNC, RegNC = @RegNC, " & _
                                         "UsaCOP = @UsaCOP, RegCOP = @RegCOP, UsaRD = @UsaRD, RegRD = @RegRD " & _
                                         "; "
                    myComm.CommandType = CommandType.Text
                    myComm.Parameters.Clear()
                    myComm.Parameters.AddWithValue("@UsaFA", pTipoDocumentos.UsaFactura)
                    myComm.Parameters.AddWithValue("@RegFA", pTipoDocumentos.RegimenFiscalFactura)
                    myComm.Parameters.AddWithValue("@UsaCP", pTipoDocumentos.UsaCartaPorte)
                    myComm.Parameters.AddWithValue("@RegCP", pTipoDocumentos.RegimenFiscalCartaPorte)
                    myComm.Parameters.AddWithValue("@UsaRH", pTipoDocumentos.UsaReciboHonorarios)
                    myComm.Parameters.AddWithValue("@RegRH", pTipoDocumentos.RegimenFiscalReciboHonorarios)
                    myComm.Parameters.AddWithValue("@UsaRA", pTipoDocumentos.UsaReciboArrendamiento)
                    myComm.Parameters.AddWithValue("@RegRA", pTipoDocumentos.RegimenFiscalReciboArrendamiento)
                    myComm.Parameters.AddWithValue("@UsaNA", pTipoDocumentos.UsaNotaCargo)
                    myComm.Parameters.AddWithValue("@RegNA", pTipoDocumentos.RegimenFiscalNotaCargo)
                    myComm.Parameters.AddWithValue("@UsaNC", pTipoDocumentos.UsaNotaCredito)
                    myComm.Parameters.AddWithValue("@RegNC", pTipoDocumentos.RegimenFiscalNotaCredito)
                    myComm.Parameters.AddWithValue("@UsaCOP", pTipoDocumentos.UsaComprobantePagoPlazos)
                    myComm.Parameters.AddWithValue("@RegCOP", pTipoDocumentos.RegimenFiscalComprobantePagoPlazos)
                    myComm.Parameters.AddWithValue("@UsaRD", pTipoDocumentos.UsaReciboDonativos)
                    myComm.Parameters.AddWithValue("@RegRD", pTipoDocumentos.RegimenFiscalReciboDonativos)
                    myConn.Open()
                    If (myComm.ExecuteNonQuery() >= 1) Then
                        Return True
                    Else
                        Return False
                    End If
                End Using
            End Using
        Catch ex As Exception
            Throw
        End Try
    End Function

    Public Function getTipoDocumentos() As TipoDocumentosBE
        Dim tmpTipoDocumentos As TipoDocumentosBE
        Try
            Using myConn As MySqlConnection = New MySqlConnection(Me.cadConex)
                Using myComm As MySqlCommand = myConn.CreateCommand()
                    myComm.CommandText = "SELECT IdtipoDoctos, UsaFA, RegFA, UsaCP, RegCP, UsaRH, RegRH, " &
                                         "UsaRA, RegRA, UsaNA, RegNA, UsaNC, RegNC, UsaCOP, RegCOP, UsaRD, RegRD " & _
                                         "FROM tipodoctos " &
                                         "ORDER BY IdtipoDoctos ASC " &
                                         "LIMIT 1 ;"
                    myComm.CommandType = CommandType.Text
                    myConn.Open()
                    Using myDR As MySqlDataReader = myComm.ExecuteReader()
                        If (myDR.HasRows()) Then
                            myDR.Read()
                            tmpTipoDocumentos = TipoDocumentosFact.Build(myDR)
                        Else
                            tmpTipoDocumentos = Nothing
                        End If
                    End Using
                End Using
            End Using
            Return tmpTipoDocumentos

        Catch ex As Exception
            Throw
        Finally
            tmpTipoDocumentos = Nothing
        End Try
    End Function

#End Region

End Class
